package org.mobilesynergies.android.epic.service.activities;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import org.mobilesynergies.epic.client.parameter.BooleanParameter;
import org.mobilesynergies.epic.client.parameter.FloatParameter;
import org.mobilesynergies.epic.client.parameter.IntParameter;
import org.mobilesynergies.epic.client.parameter.Parameter;
import org.mobilesynergies.epic.client.parameter.ParameterMap;
import org.mobilesynergies.epic.client.parameter.StringParameter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TextView;

public class UiGenerator {
	
	ParameterMap mMap = null;
	View mView = null;
	HashMap<String, Integer> mKeyIdMap = new HashMap<String, Integer>(); 

	public View generateUi(Context c, ParameterMap parameterMap) {
		LinearLayout linearLayout = new LinearLayout(c);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		ScrollView scrollView = new ScrollView(c);
		scrollView.addView(linearLayout);
		
		Set<String> set = parameterMap.keySet();
		Iterator<String> iterSet = set.iterator();
		while(iterSet.hasNext()){
			String key = iterSet.next();
			Parameter p = parameterMap.get(key);
			int id = mKeyIdMap.size();
			mKeyIdMap.put(key, id);
			if(p.getType().equalsIgnoreCase(Parameter.TYPENAME_BOOLEAN)){
				BooleanParameter param = (BooleanParameter) p;
				CheckBox cb = new CheckBox(c);
				cb.setId(id);
				cb.setChecked(param.getValue());
				cb.setText(key);
				linearLayout.addView(cb);
			} else if(p.getType().equalsIgnoreCase(Parameter.TYPENAME_STRING)){
				StringParameter param = (StringParameter) p;
				TextView tv = new TextView(c);
				tv.setText(key);
				EditText et = new EditText(c);
				et.setId(id);
				et.setText(param.getValue());
				et.setText(key);
				
				linearLayout.addView(tv);
				linearLayout.addView(et);
			} else if(p.getType().equalsIgnoreCase(Parameter.TYPENAME_FLOAT)){
				FloatParameter param = (FloatParameter) p;
				TextView tv = new TextView(c);
				tv.setText(key);
				SeekBar s = new SeekBar(c);
				s.setId(id);
				int width = (int) ((param.getMax() - param.getMin()) / param.getStepsize());
				width = Math.max(width, 1);
				s.setMax(width);
				int progressbarWidth = s.getMax();
				
				int progress = (int) ((param.getValue() - param.getMin()) / (param.getMax() - param.getMin()) * (float) progressbarWidth) ;
				s.setProgress(progress);
				linearLayout.addView(tv);
				linearLayout.addView(s);
			} else if(p.getType().equalsIgnoreCase(Parameter.TYPENAME_INT)){
				IntParameter param = (IntParameter) p;
				TextView tv = new TextView(c);
				tv.setText(key);
				SeekBar s = new SeekBar(c);
				s.setId(id);
				int width = (int) ((param.getMax() - param.getMin()) / (float)param.getStepsize());
				width = Math.max(width, 1);
				s.setMax(width);
				int progressbarWidth = s.getMax();
				int progress =  (int) ( (float) (param.getValue() - param.getMin()) / (float)(param.getMax() - param.getMin()) * (float) progressbarWidth );
				s.setProgress(progress);
				linearLayout.addView(tv);
				linearLayout.addView(s);
			}
		}
		
		mMap = parameterMap;
		LinearLayout ll = new LinearLayout(c);
		ll.setOrientation(LinearLayout.VERTICAL);
		ll.addView(scrollView);
		mView = ll;
		return ll;
	}

	public ParameterMap getValues() throws Exception {
		if((mMap==null)||(mView == null))
			return null;
		
		ParameterMap resultMap = new ParameterMap(mMap);
		Set<String> set = resultMap.keySet();
		Iterator<String> iterSet = set.iterator();
		while(iterSet.hasNext()){
			String key = iterSet.next();
			Parameter p = resultMap.get(key);
			int id = mKeyIdMap.get(key);
			if(p.getType().equalsIgnoreCase(Parameter.TYPENAME_BOOLEAN)){
				BooleanParameter param = (BooleanParameter) p;
				CheckBox cb = (CheckBox) mView.findViewById(id);
				if(cb==null){
					throw new Exception("The CheckBox view could not be found!");
				}
				param.setValue(cb.isChecked());
				
			} else if(p.getType().equalsIgnoreCase(Parameter.TYPENAME_STRING)){
				StringParameter param = (StringParameter) p;
				EditText et = (EditText) mView.findViewById(id);
				if(et==null){
					throw new Exception("The EditText view could not be found!");
				}
				param.setValue(et.getText().toString());
			} else if(p.getType().equalsIgnoreCase(Parameter.TYPENAME_FLOAT)){
				FloatParameter param = (FloatParameter) p;
				SeekBar sb = (SeekBar) mView.findViewById(id);
				if(sb ==null){
					throw new Exception("The SeekBar view could not be found!");
				}
				float fPercent = (float)sb.getProgress() / (float)sb.getMax();
				float width = (param.getMax() - param.getMin());
				float value = fPercent * width;
				param.setValue(value + param.getMin());
				
			} else if(p.getType().equalsIgnoreCase(Parameter.TYPENAME_INT)){
				IntParameter param = (IntParameter) p;
				SeekBar sb = (SeekBar) mView.findViewById(id);
				if(sb ==null){
					throw new Exception("The SeekBar view could not be found!");
				}
				
				float fPercent = (float)sb.getProgress() / (float)sb.getMax();
				int width = (param.getMax() - param.getMin());
				int value = (int) (fPercent * width);
				param.setValue(value + param.getMin());
				
			}
		}

		return resultMap;
	}
	
}
